// pages/gateway/equShip/equShip.js
const lbbService = require('../../service/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabType: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("gatewayEquShip options=", options)
    this.data.pageSize = 14
    this.data.scanPagenum = 1;
    this.data.whitePagenum = 1;
    this.data.blackPagenum = 1;
    if (options.mac) {
      this.data.gatewayMac = options.mac
    }
    if (options.equId) {
      this.setData({
        equId: options.equId,
        position: options.position,
        equHid: options.equHid
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getGatewayEquShipList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  toggleTab: function (e) {
    let type = e.currentTarget.dataset.type;
    console.log(type, this.data.tabTpye)
    this.setData({
      tabType: type
    });
  },

  getGatewayEquShipList: function () {
    let that = this
    lbbService.getGatewayEquShipList({
      gatewayMac: that.data.gatewayMac
    }, function (res) {
      if (res.result == 0) {
        that.data.scanEquAllList = res.scanList
        that.data.whiteAllList = res.whiteList
        that.data.blackAllList = res.blackList      
        that.data.scanEquAllList.forEach(el => {
          console.log("pileDetail-->", el)
          if(el.hid == that.data.equHid) {
            el.bgColor = 'gateway-equ-row-bg'
          }
        })

        that.setData({
          scanEquList: that.getEquShipList(that.data.scanEquAllList, 1),
          whiteList: that.getEquShipList(that.data.whiteAllList, 1),
          blackList: that.getEquShipList(that.data.blackAllList, 1),
          scanTotal: that.data.scanEquAllList.length,
          whiteTotal: that.data.whiteAllList.length,
          blackTotal: that.data.blackAllList.length
        })
      }
    })
  },

  getEquShipList: function (list, pagenum, dataList) {
    let startIndex = (pagenum - 1) * this.data.pageSize
    let shipList = !dataList ? new Array() : dataList
    let size = list.length >= this.data.pageSize ? this.data.pageSize : list.length
    for (var i = 0; i < size; i++) {
      let data = list[startIndex + i]
      if (data) {
        shipList.push(data)
      } else {
        break
      }
    }
    return shipList
  },

  delEquShip: function (e) {
    let that = this
    let type = e.currentTarget.dataset.tabtype;
    let mac = e.currentTarget.dataset.mac;
    let hid = e.currentTarget.dataset.hid;
    console.log("删除网关设备关系:", e.currentTarget.dataset)
    wx.showModal({
      title: '温馨提示',
      content: '确定要删除桩' + hid + '的网关绑定关系吗？',
      success(res) {
        if (res.confirm) {
          lbbService.delGatewayEquShip({
            shipType: type,
            equMac: mac
          }, function (res) {
            if (res.result == 0) {
              //删除成功
              if (type == 'scan') {
                that.setData({
                  scanEquList: that.delEquMacAndGetList(that.data.scanEquList, mac),
                  scanTotal: that.data.scanTotal - 1
                })
              } else if (type == 'white') {
                that.setData({
                  whiteList: that.delEquMacAndGetList(that.data.whiteList, mac),
                  whiteTotal: that.data.whiteTotal - 1
                })
              } else if (type = 'black') {
                that.setData({
                  blackList: that.delEquMacAndGetList(that.data.blackList, mac),
                  blackTotal: that.data.blackTotal - 1
                })
              }
              that.getGatewayEquShipList();
              wx.showToast({
                title: '删除成功',
              })
            } else {
              //删除失败
              wx.showToast({
                title: '删除失败',
              })
            }
          })
        } else if (res.cancel) {}
      }
    })

  },

  /**
   * 加入网关黑名单
   * @param {*} e 
   */
  addNoConEquShip: function(e) {
    let that = this
    let type = 'black';
    let mac = e.currentTarget.dataset.mac;
    let hid = e.currentTarget.dataset.hid;
    let gatewayMac =  that.data.gatewayMac;
    console.log("添加黑名单设备关系:", e.currentTarget.dataset)
    wx.showModal({
      title: '温馨提示',
      content: '确定要添加桩' + hid + '到黑名单？',
      success(res) {
        if (res.confirm) {
          lbbService.addGatewayEquShip({
            shipType: type,
            equMac: mac,
            gatewayMac: gatewayMac
          }, function (res) {
            if (res.result == 0) {
              if (type == 'white') {
                that.setData({
                  whiteList: that.delEquMacAndGetList(that.data.whiteList, mac),
                  whiteTotal: that.data.whiteTotal - 1
                })
              } else if (type = 'black') {
                that.setData({
                  blackList: that.delEquMacAndGetList(that.data.blackList, mac),
                  blackTotal: that.data.blackTotal - 1
                })
              }
              that.getGatewayEquShipList();
              wx.showToast({
                title: '操作成功',
              })
            } else {
              //删除失败
              wx.showToast({
                title: '操作失败',
              })
            }
          })
        } else if (res.cancel) {}
      }
    })
  },
  /**
   * 加入网关白名单
   * @param {*} e 
   */
  addConEquShip: function(e) {

    let that = this
    let type = 'white';
    let mac = e.currentTarget.dataset.mac;
    let hid = e.currentTarget.dataset.hid;
    let gatewayMac =  that.data.gatewayMac;
    console.log("添加黑名单设备关系:", e.currentTarget.dataset)
    wx.showModal({
      title: '温馨提示',
      content: '确定要添加桩' + hid + '到白名单？',
      success(res) {
        if (res.confirm) {
          lbbService.addGatewayEquShip({
            shipType: type,
            equMac: mac,
            gatewayMac: gatewayMac
          }, function (res) {
            if (res.result == 0) {
              if (type == 'white') {
                that.setData({
                  whiteList: that.delEquMacAndGetList(that.data.whiteList, mac),
                  whiteTotal: that.data.whiteTotal - 1
                })
              } else if (type = 'black') {
                that.setData({
                  blackList: that.delEquMacAndGetList(that.data.blackList, mac),
                  blackTotal: that.data.blackTotal - 1
                })
              }
              that.getGatewayEquShipList();
              wx.showToast({
                title: '操作成功',
              })
            } else {
              //删除失败
              wx.showToast({
                title: '操作失败',
              })
            }
          })
        } else if (res.cancel) {}
      }
    })

  },

  selClick: function (e) {
    let that = this
    let hid = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.tabtype;
    if (type == 'scan') {
      that.setData({
        scanEquList: that.selRowBgStyle(that.data.scanEquList, hid)
      })
    } else if (type == 'white') {
      that.setData({
        whiteList: that.selRowBgStyle(that.data.whiteList, hid)
      })
    } else if (type = 'black') {
      that.setData({
        blackList: that.selRowBgStyle(that.data.blackList, hid)
      })
    }
  },
  selRowBgStyle: function (equList, hid) {
    equList.forEach(el => {
      if (el.hid == hid) {
        el.bgColor = el.bgColor ? '' : 'gateway-equ-row-bg'
      } else {
        el.bgColor = ''
      }
    })
    return equList
  },
  delEquMacAndGetList: function (equList, mac) {
    let list = new Array()
    equList.forEach(element => {
      if (mac == element.equMac) {} else {
        list.push(element)
      }
    });
    return list
  },
  getNextPageData: function () {
    let that = this
    console.log("---->下一页---", this.data.scanPagenum, this.data.tabType, this.data.scanEquList)
    if (that.data.tabType == '') {
      if (that.data.scanEquList.length >= that.data.scanEquAllList.length) {
        wx.showToast({
          title: '已加载完',
        })
        return
      }
      that.data.scanPagenum = that.data.scanPagenum + 1
      that.setData({
        scanEquList: that.getEquShipList(that.data.scanEquAllList, that.data.scanPagenum, that.data.scanEquList),
      })
    } else if (that.data.tabType == 'white') {
      if (that.data.whiteList.length >= that.data.whiteAllList.length) {
        wx.showToast({
          title: '已加载完',
        })
        return
      }
      that.data.whitePagenum = that.data.whitePagenum + 1
      that.setData({
        whiteList: that.getEquShipList(that.data.whiteAllList, that.data.whitePagenum, that.data.whiteList),
      })
    } else if (that.data.tabType == 'black') {
      if (that.data.blackList.length >= that.data.blackAllList.length) {
        wx.showToast({
          title: '已加载完',
        })
        return
      }
      that.data.blackPagenum = that.data.blackPagenum + 1
      that.setData({
        blackList: that.getEquShipList(that.data.blackAllList, that.data.blackPagenum, that.data.blackList)
      })
    }
  }
})